<template>
  <div class="mobile-food-record-form common-mobile-view">
    <el-form ref="formRef" :model="foodStore.currentFoodRecord"
             :rules="rules" label-width="80">
      <el-form-item label="菜谱名称" prop="name">
        <el-select
            v-model="foodStore.currentFoodRecord.name"
            @focus="foodStore.queryFoodNameList()"
            :disabled="foodStore.foodRecordApiType === 'UPDATE'"
            placeholder="请选择菜谱名称">
          <el-option
              v-for="(item, index) in foodStore.foodNameList"
              :key="index" :label="item" :value="item"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="完成时间" prop="date">
        <el-date-picker
            v-model="foodStore.currentFoodRecord.date"
            type="date" :editable="false" placeholder="请选择完成时间"
            value-format="YYYY-MM-DD"/>
      </el-form-item>

      <el-form-item label="完成人" prop="person">
        <el-select
            v-model="foodStore.currentFoodRecord.person"
            placeholder="请选择完成人">
          <el-option
              v-for="(item, index) in ['哥哥', '宝宝']"
              :key="index" :label="item" :value="item"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="图片">
        <upload-image v-model="foodStore.currentFoodRecord.imageUrl"
                      :service-url="fileServiceUrl"
                      :image-limit="1"
                      :service-file-root-path="serviceFileRootPath"
                      :image-path="foodStore.foodImagePath"/>
      </el-form-item>
    </el-form>

    <div class="button-container">
      <el-button type="primary" @click="confirmClick">确认</el-button>
      <el-button type="danger" @click="deleteClick">取消</el-button>
      <el-button type="info" @click="cancelClick">取消</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { UploadImage } from 'vue3-common'
import { useFoodStore } from '@/store/food.ts'
import { ref, reactive } from 'vue'
import { FormRules } from 'element-plus'
import { foodRecordRules } from '@/utils/element/elRules.ts'
import { fileServiceUrl, serviceFileRootPath } from '@/utils'
import { commonElMessageBox } from 'vue3-common/utils/elUtil'

const rules = reactive<FormRules>(foodRecordRules)

const formRef = ref()
const foodStore = useFoodStore()

/**
 * 点击确认按钮
 */
const confirmClick = () => {
  formRef.value.validate().then(() => {
    foodStore.handleFoodRecordApi(foodStore.currentFoodRecord)
    history.back()
  })
}

const cancelClick = () => {
  history.back()
}

const deleteClick = () => {
  commonElMessageBox('是否确认删除该记录?').then(() => {
    foodStore.foodRecordApiType = 'DELETE'
    foodStore.handleFoodRecordApi(foodStore.currentFoodRecord)
    history.back()
  })
}
</script>

<style lang="scss">
.mobile-food-record-form {
  display: flex;
  flex-direction: column;
  gap: 10px;

  .button-container {
    align-self: center;
  }
}</style>
